﻿using Orchard;
using Orchard.Users.Models;
using Orchard.Themes;
using Orchard.ContentManagement;
using System.Web.Mvc;
using System.Linq;
using NineSeas.Services;
using NineSeas.Models;
using Orchard.Core.Common.Models;
using Orchard.Mvc;
using Orchard.DisplayManagement;
using Orchard.Security;
using Orchard.Localization;

namespace NineSeas.Controllers
{
    public class AuthorController : Controller
    {
        private const int _defaultPagesize = 8;
        private readonly IOrchardServices _services;
        private readonly IBookService _bookService;
        private readonly ISocialStatisticService _socialStatisticService;
        private readonly IAuthenticationService _authenticationService;

        private Localizer T { get; set; }

        private dynamic Shape { get; set; }

        public AuthorController(IShapeFactory shapeFactory,
            IAuthenticationService authenticationService,
            IOrchardServices services,
            ISocialStatisticService socialStatisticService,
            IBookService bookService)
        {
            this._services = services;
            this._bookService = bookService;
            this._socialStatisticService = socialStatisticService;
            this.Shape = shapeFactory;
            this._authenticationService = authenticationService;
            T = NullLocalizer.Instance;
        }

        [Themed]
        public ActionResult Index(string name, int pageIndex = 0, int pageSize = _defaultPagesize)
        {
            var user = _services.ContentManager.Query<UserPart, UserPartRecord>()
                .Where(u => u.UserName == name)
                .List()
                .FirstOrDefault();

            var paging = new PaginationInfo(pageIndex, pageSize);

            return AuthorPage(user, paging);
        }


        [Themed]
        public ActionResult MyPage(int pageIndex = 0, int pageSize = _defaultPagesize)
        {
            var user = _authenticationService.GetAuthenticatedUser();
            if (_authenticationService.GetAuthenticatedUser() == null)
                throw new OrchardSecurityException(T("Login required"));

            var paging = new PaginationInfo(pageIndex, pageSize);

            return AuthorPage(user, paging);
        }

        private ActionResult AuthorPage(IUser user, PaginationInfo paging)
        {
            var books = _bookService.GetBooksByAuthor(user.Id, paging);
            var author = user != null ? user.As<AuthorProfilePart>() : null;
            var pager = Shape.Pager(paging).TotalItemCount(paging.TotalRecords);
            var partVersion = user != null ? user.As<ContentPart<CommonPartVersionRecord>>() : null;

            return new ShapeResult(this, _services.New.Authors_Author_Details(
                    User: user,
                    Author: author,
                    Books: books,
                    Paging: paging,
                    Pager: pager,
                    JoinedDate: partVersion != null ? partVersion.Record.CreatedUtc.Value.ToString("dd/MM/yyyy hh:mm:ss") : "",
                    IsLiked: _socialStatisticService.IsLiked(author)
                ));
        }
    }
}